package com.offcn.project.controller;

import com.alibaba.fastjson.JSON;
import com.offcn.common.response.AppResponse;
import com.offcn.common.vo.BaseVo;
import com.offcn.project.pojo.TReturn;
import com.offcn.project.service.ProjectCreateService;
import com.offcn.project.vo.req.ProjectBaseInfoVo;
import com.offcn.project.vo.req.ProjectReturnInfoVo;
import com.offcn.project.vo.req.ProjectStoreToRedisVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(tags="项目创建，四步完成项目添加")
@RequestMapping("/create")
@RestController
public class ProjectCreateController {

    @Autowired
    private ProjectCreateService projectCreateService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @ApiOperation("项目添加的第1步-初始化项目")
    @PostMapping("/init")
    public AppResponse<String> init(BaseVo baseVo){
        // 获取发起者ID
        String accessToken = baseVo.getAccessToken();
        String memberIdStr = redisTemplate.opsForValue().get(accessToken);
        if(StringUtils.isEmpty(memberIdStr)){
            return AppResponse.fail("用户没有登录，请登录之后重试");
        }
        Integer memberId = Integer.parseInt(memberIdStr);
        String projectToken = projectCreateService.initCreateProject(memberId);
        return AppResponse.ok(projectToken);


    }

    @ApiOperation("项目添加的第2步-添加项目的基本信息")
    @PostMapping("/addBaseInfo")
    public AppResponse<String> addBaseInfo(ProjectBaseInfoVo baseInfoVo){
        // 是否登录
        String memberIdStr = redisTemplate.opsForValue().get(baseInfoVo.getAccessToken());
        if(StringUtils.isEmpty(memberIdStr)){
            return AppResponse.fail("用户没有登录，请登录之后重试");
        }
        // 从redis获取项目
        String redisVoStr = redisTemplate.opsForValue().get(baseInfoVo.getProjectToken());
        ProjectStoreToRedisVo redisVo = JSON.parseObject(redisVoStr, ProjectStoreToRedisVo.class);
        // 添加项目基本信息
        BeanUtils.copyProperties(baseInfoVo,redisVo);
        // 将项目写入到redis
        redisVoStr = JSON.toJSONString(redisVo);
        redisTemplate.opsForValue().set(baseInfoVo.getProjectToken(),redisVoStr);
        return AppResponse.ok(baseInfoVo.getProjectToken());

    }

    @ApiOperation("项目添加的第3步-添加项目回报信息")
    @PostMapping("/addReturnInfo")
    public AppResponse<String> addReturnInfo(@RequestBody List<ProjectReturnInfoVo> returns){
        // 是否登录
        String memberIdStr = redisTemplate.opsForValue().get(returns.get(0).getAccessToken());
        if(StringUtils.isEmpty(memberIdStr)){
            return AppResponse.fail("用户没有登录，请登录之后重试");
        }
        // 从redis中取项目
        String projectToken = returns.get(0).getProjectToken();
        String redisVoStr = redisTemplate.opsForValue().get(projectToken);
        ProjectStoreToRedisVo redisVo = JSON.parseObject(redisVoStr, ProjectStoreToRedisVo.class);
        List<TReturn> returnList = new ArrayList<>();
        for(ProjectReturnInfoVo infoVo : returns){
            TReturn tReturn = new TReturn();
            BeanUtils.copyProperties(infoVo,tReturn);
            returnList.add(tReturn);
        }
        redisVo.setProjectReturns(returnList);
       // 写入到redis
        redisVoStr = JSON.toJSONString(redisVo);
        redisTemplate.opsForValue().set(projectToken,redisVoStr);
        return AppResponse.ok(projectToken);

    }

    @ApiOperation("项目添加的第4步-添加项目到数据库")
    @PostMapping("/storeToDB")
    public AppResponse<String> storeToDB(String accessToken,String projectToken){
        // 是否登录
        String memberIdStr = redisTemplate.opsForValue().get(accessToken);
        if(StringUtils.isEmpty(memberIdStr)){
            return AppResponse.fail("用户没有登录，请登录之后重试");
        }
        // 获取redis中的project
        String redisVoStr = redisTemplate.opsForValue().get(projectToken);
        ProjectStoreToRedisVo redisVo = JSON.parseObject(redisVoStr, ProjectStoreToRedisVo.class);
        // 完成数据库的写入
        projectCreateService.saveProjectInfo(redisVo);
        return AppResponse.ok("成功");

    }
}
